package com.hd.trans.share;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hd.trans.R;
import com.hd.trans.widgets.RxCountDownTimer;


/**
 * 没有取消按钮的进度框
 */
public class ProgressDialogD extends Dialog {
    private final String tag = "ProgressDialogD";


    // 显示文本
    @StringRes
    private int mContentId = R.string.null_string;

    // 当前进度
    private int mProgress = 0;
    // 最大进度
    private int mMax = 100;

    // 虚拟进度，自增长
    private boolean mAutoInCrease ;

    private boolean mShowProgress = true;
    private TextView tvContent, tvProgressNum;
    @Nullable
    private RxCountDownTimer mTimer = null;

    public ProgressDialogD(Context context) {
        super(context, R.style.FullScreenProgressDialog);
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_progress_d);
        tvContent = findViewById(R.id.share_tv_content);
        tvProgressNum = findViewById(R.id.share_tv_progress_num);
    }

    public ProgressDialogD setContent(@StringRes int contentId) {
        this.mContentId = contentId;
        tvContent.setText(mContentId);
        return this;
    }

    public ProgressDialogD setAutoInCrease(boolean autoInCrease) {
        this.mAutoInCrease = autoInCrease;
        return this;
    }

    public ProgressDialogD setProgress(int progress) {
        return setProgress(progress, mMax);
    }

    /**
     * @param progress 当前进度
     * @param max      最大进度
     * @return
     */
    public ProgressDialogD setProgress(int progress, int max) {
//        this.mProgress = Math.min(progress, max);
//        tvProgressNum.setText(String.valueOf(progress));

        return this;
    }

    public float getProgress() {
        return this.mProgress;
    }

    public ProgressDialogD setMax(int max) {
        this.mMax = max;
        setProgress(mProgress, mMax);
        if (mTimer != null) {
            mTimer.cancel();
        }
        return this;
    }

    public ProgressDialogD setShowProgress(boolean showProgress) {
        this.mShowProgress = showProgress;

        /*if (mShowProgress) {
            tvProgressNum.setVisibility(View.VISIBLE);
        } else {
            tvProgressNum.setVisibility(View.INVISIBLE);
        }*/

        return this;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setCanceledOnTouchOutside(false);


    }


    @Override
    public void show() {
        super.show();
        Log.d(tag, "show");
        setShowProgress(mShowProgress);

        tvContent.setText(mContentId);

       /* mTimer = new RxCountDownTimer(1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (mAutoInCrease) {
                    setProgress(getProgress() + 1);
                }
            }

            @Override
            public void onFinish() {
                // do nothing
            }
        };*/
       /* if (mAutoInCrease) {
            mTimer.start();
        }*/
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mTimer != null) {
            mTimer.cancel();
        }
    }
}